package com.huazi.www.frontdesk.servlet;

import com.alibaba.fastjson.JSON;
import com.huazi.user.registration.entity.User;
import com.huazi.user.registration.service.Imp.LoginService;
import com.huazi.www.frontdesk.javaBase.FrontdeskCardInfo;
import com.huazi.www.frontdesk.javaBase.PageInfo;
import com.huazi.www.frontdesk.service.impl.FrontdeskCardServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 缴费
 */
public class PageFrontdeskCardServlet extends HttpServlet {
    FrontdeskCardServiceImpl frontdeskCardService = new FrontdeskCardServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/thml;charse=UTF-8");//修改respones域的编码集
        req.setCharacterEncoding("UTF-8");//修改request域的编码集
        resp.setCharacterEncoding("UTF-8");//修改respones域的编码集
        PrintWriter out = resp.getWriter();//创建输出对象
        String operation = req.getParameter("operation");//获取操作码 operation
        if (operation == null) {
            operation = "";
        }
//        System.out.println(operation);
        if (operation.equals("inquire") || operation.equals("")) {
            String id = req.getParameter("id");//获取传过来的id
            String curr = req.getParameter("curr");//获取当前码curr
            if (id == null || id == "") {
                id = "0";
            }
            if (curr == null || curr == "") {
                curr = "1";
            }
            PageInfo<FrontdeskCardInfo> pagefrontdeskCard = frontdeskCardService.pagefrontdeskcard(Integer.parseInt(id), Integer.parseInt(curr));//new frontdeskCardService类pagefrontdeskcard方法
            req.setAttribute("pagefrontdeskCard", pagefrontdeskCard); //保存pagefrontdeskCard对象
            req.setAttribute("pagefrontdeskCardId", id); //保存id对象
            req.getRequestDispatcher("frontdesk/reissue.jsp").forward(req, resp);//转发至reissue.jsp页面
        } else if (operation.equals("idquery")) {
            Integer id = Integer.parseInt(req.getParameter("id"));//获取传过来的值 id并转换为int类型
            List<FrontdeskCardInfo> idqueue = frontdeskCardService.idqueue(id);//new frontdeskCardService类idqueue方法
            String isidqueue = JSON.toJSONString(idqueue);//转JSON
            out.println(isidqueue);//输出到页面
        } else if (operation.equals("rechargeable")) {
            double amount = Double.parseDouble(req.getParameter("amount"));//获取传过来的值 amount并转换为Double类型
            Integer id = Integer.parseInt(req.getParameter("id"));//获取传过来的值 id并转换为int类型
            boolean recharging = frontdeskCardService.recharging(id, amount);//new frontdeskCardService类recharging方法
            String isrecharging = JSON.toJSONString(recharging);//转JSON
            String account = req.getParameter("id");
            if (account != null) {
                LoginService service = new LoginService();
                User user = service.selectUser(account);
                HttpSession session = req.getSession();
                session.setAttribute("user", user);//保存用户信息
            }
            out.println(isrecharging);//输出到页面
        }
        out.flush();//清空缓存区
        out.close();//关闭缓存区

    }
}
